<?php


namespace app\cms\community;

use app\cms\community\lib\Query\elasticsearchQuery;
use app\cms\model\carouselModel;
use app\cms\model\companyModel;
use app\cms\model\formModel;
use app\cms\model\friendModel;
use app\cms\model\navModel;
use app\cms\model\siteModel;
use iflow\Response;
use iflow\Router\Controller;
use iflow\Router\implement\Request\GetMapping;

#[Controller('cms/api/community/info')]
class info
{
    /**
     * 获取站点信息
     * @return Response
     */
    #[GetMapping]
    public function getSiteInfo(): Response
    {
        return siteModel::getSiteInfo();
    }

    /**
     * 获取企业信息
     * @return Response
     */
    #[GetMapping]
    public function getCompanyInfo(): Response
    {
        return companyModel::getCompanyInfo();
    }

    /**
     * 获取导航栏信息
     * @return Response
     */
    #[GetMapping]
    public function getNavInfo(): Response
    {
        return (new navModel()) -> getNav();
    }

    /**
     * 获取友链信息
     * @param string $site_title
     * @return Response
     */
    #[GetMapping]
    public function getFriendInfo(string $site_title = ''): Response
    {
        return friendModel::getFriend($site_title);
    }

    /**
     * 获取轮播图
     * @param int $carousel_group_id
     * @return Response
     */
    #[GetMapping('get_carousel/<[0-9]{1,8}:carousel_group_id>')]
    public function getCarousel(int $carousel_group_id = 0): Response
    {
        $rows = carouselModel::where([
            'carousel_group_id' => $carousel_group_id
        ]) -> select() -> toArray();
        if ($rows) return message() -> success('查询成功', $rows);
        return message() -> nodata('暂无数据');
    }

    /**
     * 根据表单 id 获取数据
     * @param int $form_id
     * @param int $id
     * @param int $user_id
     * @return Response
     */
    #[GetMapping('get_diy_form_info/<[0-9]{1,8}:form_id>/<[0-9]{1,8}:id>')]
    public function getDiyFormInfo(int $form_id = 0, int $id = 0, int $user_id = 0): Response
    {
        return (new formModel()) -> getHasModelDataList($form_id, id: $id, user_id: $user_id);
    }

    /**
     * 根据表单搜索数据
     * @param int $form_id
     * @param string $keyword
     * @param string $order
     * @param int $pageSize
     * @return Response
     */
    #[GetMapping('search_diy_form_info/<[0-9]{1,8}:form_id>')]
    public function searchDiyFormInfo(int $form_id = 0, string $keyword = "", string $order = 'create_time@desc', int $pageSize = 15): Response
    {
        $formModel = new formModel();
        $form = $formModel -> getForm($form_id);
        if ($form) {
            [$f, $d] = explode('@', $order);
            $query = $formModel -> table($form['form_name']);

            if ($form['title_field'] !== '-1') {
                $query -> whereLike($form['title_field'], "%{$keyword}%") -> order([
                    $f => $d
                ]);
            }

            $query = $query -> paginate([
                'list_rows' => $pageSize
            ]);

            return message() -> success('查询成功', $query -> items(), [
                'count' => $query -> total(),
                'page' => $query -> lastPage()
            ]);
        }
        return message() -> nodata('表模型不存在');
    }

    /**
     * 根据表单id 进行elasticSearch搜索
     * @param int $form_id
     * @param string $keyword
     * @param string $order
     * @return Response
     */
    #[GetMapping]
    public function elasticSearchForm(int $form_id = 0, string $keyword = "", string $order = 'create_time@desc'): Response
    {
        $query = new elasticsearchQuery();
        $order = explode('@', $order);

        $query -> setFormId($form_id)
            -> setKeyword($keyword)
            // 设置 ES 的索引名称
            -> setIndexName('');

        if (count($order) > 1) {
            $query -> setSort([
                $order[0] => [
                    'order' => $order[1]
                ]
            ]);
        }
        return $query -> query();
    }
}